package org.spring.userservice.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.spring.userservice.entity.UserEntity;
import org.spring.userservice.feign.UserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@SessionAttributes("userId")
public class SesscionController {
	
 	@Autowired private StringRedisTemplate stringRedisTemplate;
    @Autowired private RedisTemplate<String, ?> redisTemplate;
    @Autowired private UserServiceFeign userServiceFeign;
    
	@RequestMapping("/setString")
	@ResponseBody
	public String setString() {
		List<String> list = new ArrayList<>();
		list.add("aaaa");
		list.add("test");
//		批量删除KEY
		stringRedisTemplate.delete(list);
//		保存
		stringRedisTemplate.opsForValue().set("stirngaaaa", "2221");
//		设置过期时间
		stringRedisTemplate.expire("stirngaaaa", 80, TimeUnit.SECONDS);
//		保存并设置过期时间
		stringRedisTemplate.opsForValue().set("stirngtest", "test", 80, TimeUnit.SECONDS);
		System.out.println("123123123");
		return stringRedisTemplate.opsForValue().get("stirngaaaa");
	}
	@RequestMapping("/setHash")
	@ResponseBody
	public List<?> setHash() {
		redisTemplate.opsForHash().put("hashtest", "test0", "deaad0");
		redisTemplate.opsForHash().put("hashtest", "test2", "deaad1");
		return redisTemplate.opsForHash().values("hashtest");
//		redisTemplate.
	}
	 
	@RequestMapping("/cacheHash")
	@ResponseBody
//	 value缓存名、 key缓存键值、 condition满足缓存条件、unless否决缓存条件 
	@Cacheable(value = "models")
	public Map<String, Object> cacheHash(String fs){
		Map<String, Object> map = new HashMap<>();
		map.put("user1", new UserEntity());
		UserEntity userEntity = new UserEntity();
		userEntity.setUserName(userEntity.getUserName() + "0");
		map.put("user2", userEntity);
		System.out.println("是否成功调用cache");
		return map;
	}
	
	@RequestMapping("/cacheAraay")
	@ResponseBody
	@Cacheable(value="cacheAraay")
	public List<?> cacheAraay(String id){
		List<UserEntity> userList = new ArrayList<>();
		userList.add(new UserEntity());
		UserEntity userEntity = new UserEntity();
		userEntity.setUserName(userEntity.getUserName() + "0");
		System.out.println("是否成功调用cache");
		return userList;
	}
	
	@RequestMapping("/getUser")
	@ResponseBody
	@Cacheable(value="getUser")
	public UserEntity getUser(String userName) {
		UserEntity entity = new UserEntity();
		entity.setUserName(userName);
		System.out.println("123123");
		return entity;
	}
	
	@ResponseBody
	@RequestMapping(value = "/setSession")  
	public String setSession(HttpServletRequest request) {
		System.out.println(request.getHeader("x-auth-token"));
		request.getSession().setAttribute("request Url", request.getRequestURL());  
		
		return request.getSession().getId();
	}
	
	/**
	 * 使用另外一个浏览器调用，并传出setSession的id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getSession")  
	public String getSession(HttpServletRequest request) {
		System.out.println(request.getSession().getId());
		return request.getSession().getAttribute("request Url").toString();
	}
	
	@RequestMapping(value = "/feignTest")  
	@ResponseBody
	public String feignTest(String name, HttpServletRequest request) {
		request.setAttribute(name, request.getRequestURL());
		return userServiceFeign.feignTest(name);
	}
	
	@RequestMapping(value = "/getFeign")  
	@ResponseBody
	public String getFeign(String name, HttpServletRequest request) {
		return request.getAttribute(name).toString();
	}
	
}
